package com.yy.tools.date
{
	/**
	 * 
	 * @author yy
	 * 
	 */
	public class DateFormat
	{
		public function DateFormat()
		{
		}
		
		/**
		 *字符串转日期 
		 * @param str  要转的日期字符串 如:2005:03:5 10.2.3.1
		 * @param format	传入的日期的格式如 y:m:d h:mi:s:ms   y:年m:月d日 h小时:mi分钟:s秒:ms毫秒
		 * @param splitStr 分割符 ": ."  第一个字符为用于年月日之间的分割 第二个为（年、月、日）与 （时、分、秒、毫秒）之间的分割
		 * @return 返回Date类型
		 * 注：format 和splitStr的格式要匹配
		 * eg: 
		 * var date:Date = DateFormat.stringToDate("2005:03:5 10.2.3.1", "y:m:d h.mi.s.ms", ": .");
		 * var date:Date = DateFormat.stringToDate("5:03:2005 10:2:3:1", "d:m:y h:mi:s:ms", ": :");
		 */		
		public static function stringToDate(str:String, format:String= "y:m:d h:mi:s:ms", splitStr:String=": :"):Date {
			if(splitStr.length != 3){
				throw new Error("传入分隔符格式错误！");
			}
			var a:String = splitStr.charAt(0)
			var b:String = splitStr.charAt(1)
			var c:String = splitStr.charAt(2)
			var y:int;
			var m:int;
			var d:int;
			var h:int;
			var mi:int;
			var s:int;
			var ms:int;
			var strArr:Array = str.split(b);
			var fArr:Array = format.split(b);
			var fArr1:Array = String(fArr[0]).split(a);
			var fArr2:Array;
			var strArr1:Array = String(strArr[0]).split(a);
			var strArr2:Array;
			var i:int = 0;
			var len:int = strArr1.length;
			//解析年月日
			for(i=0; i<len; i++) {
				switch(String(fArr1[i])) {
					case "y":
						y = strArr1[i];
						break;
					case "m":
						m = strArr1[i];
						break;
					case "d":
						d = strArr1[i];
						break;
				}
			}
			//解析时分秒
			if(strArr[1]) {
				strArr2 = String(strArr[1]).split(c);
				fArr2 = String(fArr[1]).split(c);
				len = strArr2.length;
				for(i=0; i<len; i++) {
					switch(String(fArr2[i])) {
						case "h":
							h = strArr2[i];
							break;
						case "mi":
							mi = strArr2[i];
							break;
						case "s":
							s = strArr2[i];
							break;
						case "ms":
							ms = strArr2[i];
							break;
					}
				}
			}else {
				h = mi = s = ms = 0;
			}
			var date:Date;
			date = new Date(y,m-1,d,h,mi,s,ms);
			return date;
		}
	}
}